import { ElMessage } from "element-plus";
import { LOGIN_URL } from "@/config";
import { useUserStore } from "@/stores/modules/user";

/**
 * @description: 校验网络请求状态码
 * @param {Number} status
 * @return void
 */
export const checkStatus = (status: number, data: any = { message: "" }) => {
  switch (status) {
    case 400:
      ElMessage.error("操作失败！" + data.message);
      break;
    case 401:
      ElMessage.error("登录失效！请您重新登录");
      const userStore = useUserStore();
      userStore.setToken("");
      localStorage.removeItem("token");
      // 重定向到登陆页
      window.location.replace(LOGIN_URL);
      break;
    case 403:
      ElMessage.error(data.message);
      break;
    case 404:
      ElMessage.error(data.message);
      break;
    case 405:
      ElMessage.error(data.message);
      break;
    case 408:
      ElMessage.error(data.message);
      break;
    case 500:
      ElMessage.error(data.message);
      break;
    case 502:
      ElMessage.error(data.message);
      break;
    case 503:
      ElMessage.error(data.message);
      break;
    case 504:
      ElMessage.error(data.message);
      break;
    default:
      ElMessage.error(data.message);
  }
};
